package com.dd.ent.car.common.player;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.dd.ent.car.R;


/**
 * Author:LiuJianSheng
 * Date:2016/6/21
 */
public class MediaPlayerCompletionView extends FrameLayout implements MediaPlayerStateChangeListener {
    private ThunderMediaPlayer mediaPlayer;
    private ImageView mIvPlay;
    private ImageView mIvReplay;
    private TextView mTvReplay;
    private ImageView mPrevPlayBtn;//上一个
    private ImageView mNextPlayBtn;//下一个
    private OnControllerClickListener mClickListener;
    private OnClickListener mReplayClickListener;

    public MediaPlayerCompletionView(Context context) {
        super(context);
        initView(context);
    }

    public MediaPlayerCompletionView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public MediaPlayerCompletionView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        View rootView = LayoutInflater.from(context).inflate(R.layout.media_player_completion_view, this, true);
        mIvReplay = (ImageView) rootView.findViewById(R.id.replay);
        mIvReplay.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mReplayClickListener == null) {
                    if (mediaPlayer != null) {
                        mediaPlayer.rePlay();
                        //回调到activity处理一下事情
                        if(mClickListener != null){
                            mClickListener.onClickReplayBtn(v);
                        }
                    }
                } else {
                    mReplayClickListener.onClick(v);
                }
            }
        });
        mTvReplay = (TextView) rootView.findViewById(R.id.tv_replay);
        mIvPlay = (ImageView) rootView.findViewById(R.id.iv_play);
        mPrevPlayBtn = (ImageView) rootView.findViewById(R.id.prev_play_btn);
        mPrevPlayBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mClickListener != null) {
                    mClickListener.onClickPrevBtn(v);
                }
            }
        });
        mNextPlayBtn = (ImageView) rootView.findViewById(R.id.next_play_btn);
        mNextPlayBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mClickListener != null) {
                    mClickListener.onClickNextBtn(v);
                }
            }
        });
        setVisibility(GONE);
    }

    public void bindMediaPlayer(ThunderMediaPlayer mediaPlayer) {
        this.mediaPlayer = mediaPlayer;
    }

    @Override
    public void onStateChange(MediaPlayerState oldState, MediaPlayerState newState) {
        switch (newState) {
            case PLAYBACK_COMPLETED:
                int viewType = mediaPlayer.getViewType();
                setVisibility(View.VISIBLE);
                if (viewType == ThunderMediaPlayer.TYPE_AUTO_PLAY) {
                    showPlayBtn();
                } else {
                    showReplayBtn();
                }
                break;
            default:
                setVisibility(GONE);
        }
    }

    private void showReplayBtn() {
        mIvPlay.setVisibility(View.GONE);
        mIvReplay.setVisibility(View.VISIBLE);
        mTvReplay.setVisibility(View.VISIBLE);
    }

    private void showPlayBtn() {
        mIvPlay.setVisibility(View.VISIBLE);
        mIvReplay.setVisibility(View.GONE);
        mTvReplay.setVisibility(View.GONE);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        return true;
    }

    public void setPrevPlayBtnVisiable(boolean isVisible) {
        if (isVisible) {
            mPrevPlayBtn.setVisibility(VISIBLE);
        } else {
            mPrevPlayBtn.setVisibility(GONE);
        }
    }

    public void setNextPlayBtnVisiable(boolean isVisible) {
        if (isVisible) {
            mNextPlayBtn.setVisibility(VISIBLE);
        } else {
            mNextPlayBtn.setVisibility(GONE);
        }
    }

    public void setOnControllerClickListener(OnControllerClickListener listener) {
        this.mClickListener = listener;
    }

    public void setReplayClickListener(OnClickListener replayClickListener) {
        mReplayClickListener = replayClickListener;
    }

}
